package com.liu.controller;

import com.liu.bean.Enterprise;
import com.liu.bean.XuFei;
import com.liu.bean.query.EnterpriseQuery;
import com.liu.common.bean.PageBean;
import com.liu.common.bean.ResultBean;
import com.liu.common.controller.BaseController;
import com.liu.common.crud.IService;
import com.liu.common.valid.ISave;
import com.liu.common.valid.IUpdate;
import com.liu.service.EnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "企业管理")
public class EnterpriseController extends BaseController {

    @Autowired
    EnterpriseService enterpriseService;

    @ApiOperation("保存企业")
    @PostMapping("/enterprise")
    public ResultBean save(@Validated(ISave.class) @RequestBody Enterprise enterprise, BindingResult result){
        validate(result);
        int rs = enterpriseService.save(enterprise);
        return saveResult(rs);
    }

    @ApiOperation("查询企业")
    @PostMapping("/enterprise/list")
    public ResultBean<PageBean> query(@RequestBody EnterpriseQuery enterpriseQuery){
        PageBean pageBean = enterpriseService.query(enterpriseQuery.toMap());
        return success("企业列表",pageBean);
    }

    @ApiOperation("修改企业")
    @PutMapping("/enterprise")
    public ResultBean update(@Validated(IUpdate.class) @RequestBody Enterprise enterprise,BindingResult result){
        int rs = enterpriseService.update(enterprise);
            return updateResult(rs);
    }

    @ApiImplicitParam(name = "id",value = "企业id",required = true,dataTypeClass = Integer.class)
    @ApiOperation("删除企业")
    @DeleteMapping("/enterprise")
    public ResultBean delete(@RequestParam(name = "id",required = true) int id){
        int rs = enterpriseService.delete(id);
        return deleteResult(rs);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "企业id",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "empNum",value = "可创建的员工数",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "accountNum",value = "可创建的账套数",required = true,dataTypeClass = Integer.class),
    })
    @PutMapping("/enterprise/auth")
    @ApiOperation("权限")
    public ResultBean auth(@RequestParam(name = "id",required = true) int id,int empNum,int accountNum){
        int rs = enterpriseService.auth(id,empNum,accountNum);
        return saveResult(rs);
    }

    @ApiOperation("续费")
    @PutMapping("/enterprise/xvfei")
    public ResultBean XuFei(@Validated @RequestBody XuFei xuFei,BindingResult result){
        validate(result);
        int rs = enterpriseService.xvfei(xuFei);
        return rs==1?success("续费成功"):fail("续费失败");
    }

    @ApiImplicitParam(name = "id", value = "企业id",required = true)
    @ApiOperation("企业资金信息")
    @GetMapping("/enterprise/{id}")
    public ResultBean get(@PathVariable(name = "id") int id){
        Map map = enterpriseService.queryMoney(id);
        return success("资金信息",map);
    }

    @ApiOperation("企业导出")
    @PostMapping("/enterprise/export")
    public ResultBean export(@RequestBody Map map){
        String url = enterpriseService.export(map);
        return success("导出企业",url);
    }


}
